home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dev / insight_dev.idb / usr / share / Insight / bin / make_bldsgml.z / make_bldsgml
Text File  |  2002-10-15  |  14KB  |  467 lines

  1. #!/usr/bin/perl5
  2.  
  3. #
  4. # Copyright 2002, Silicon Graphics, Inc.
  5. # All Rights Reserved.
  6. #
  7. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  8. # the contents of this file may not be disclosed to third parties, copied or
  9. # duplicated in any form, in whole or in part, without the prior written
  10. # permission of Silicon Graphics, Inc.
  11. #
  12. # RESTRICTED RIGHTS LEGEND:
  13. # Use, duplication or disclosure by the Government is subject to restrictions
  14. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  15. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  16. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  17. # rights reserved under the Copyright Laws of the United States.
  18. #
  19. ###############################################################################
  20. #
  21. # Creates buildable SGML source from parent SGML file and components.
  22. # Called by commondocrules for "make book" operation.
  23. #
  24. # Requires perl5 to run correctly.
  25.  
  26. $| = 1;
  27.  
  28. ################################################################################
  29. ### Subroutine to run system command lines, check for errors, & issue messages
  30. ###    $1 - $n = The system command lines to run
  31.  
  32. sub RunIt {
  33.     my($CmdLine) = @_;
  34.     my($ECode) = 0;
  35.  
  36.     $ECode = system ("$CmdLine");
  37.     if ($ECode) {
  38.         if ($ECode > 255)  { $ECode /= 256 ; }
  39.         print STDERR "\a\n$Name:  $ECode from:\n\t$CmdLine\n" ;
  40.         exit($ECode);
  41.     }
  42. }
  43.  
  44. ################################################################################
  45. ### Cleans up SGML files:  Removes Adept processing instructions and joins
  46. ### tags broken across lines
  47. ###    $1 = Name of file
  48. ###    $2 = String containing one complete SGML file
  49.  
  50. sub CleanUp {
  51.    my ($FileName, $Buff) = @_ ;
  52.    my (@Tmp, $Ent, %Tmp, @Ents, $Tmp) ;
  53.  
  54.    ### change the \r\n output from spam into single carriage-returns
  55.    $$Buff =~ s\r\r\n\ng ;  ## sometimes two \r passthru by spam
  56.    $$Buff =~ s\r\n\ng ;
  57.    $$Buff =~ s\rg ;
  58.  
  59.    ### Join line-broken tags:
  60.    ##  SGML allows this: <section
  61.    ##                    id="thing1"
  62.    ##                    >
  63.    ##  But processing in scripts simplified if < to > on one line.
  64.    while ($$Buff =~ s#(<[^>]+)[\n]#$1 #g)
  65.       { $NoOp = 1 ; }
  66.  
  67.    ### Delete all SGML-standard comments:
  68.    $$Buff =~ s#<!--.*?-->##gs;
  69.    $$Buff =~ s#<!>##gs ;
  70.  
  71.    ### Change all carriage-return processing instructions to newline elements:
  72.    $$Buff =~ s#<\?Pub\s*_(newline>)#<$1#g ;
  73.  
  74.    ### Remove Adept processing instructions:
  75.    $$Buff =~ s#\n<\?[^>]+>\n#\n#g ;
  76.    $$Buff =~ s#<\?[^>]+>##g ;
  77.  
  78.    ### Fix <stuff> problem for mkbook parser:
  79.    ##  first change all <sgmltag> to SGML_TAGOsgmltagSGML_TAGC :
  80.    $$Buff =~ s#<([^>]*)>#SGML_TAGO$1SGML_TAGC#gs ;
  81.    ##  change leftover > chars to > :
  82.    $$Buff =~ s#>#>#gs ;
  83.    ##  change delimiters back:
  84.    $$Buff =~ s#SGML_TAGO#<#gs ;
  85.    $$Buff =~ s#SGML_TAGC#>#gs ;
  86.  
  87.     # todo: verify this is needed
  88.    ### Change certain character entities to literals (stylesheet bugs):
  89.    $$Buff =~ s–-gs ;
  90.    $$Buff =~ s—--gs ;
  91.    $$Buff =~ s  gs ;
  92.    $$Buff =~ s…...gs ;
  93.  
  94.    ### Change entityref values to include .gif extension:
  95.    ##  Might have <graphic entityref="abc" magnification="90"></graphic>
  96.    ##  or have <graphic magnification="90" entityref="abc"></graphic>
  97.    $$Buff =~ s#<graphic([^>]*)entityref\s*=\s*\"([^"]*)\"#<graphic$1entityref=\"$2.gif\"#igs ;
  98.  
  99.    ## Adept editor makes specific breaks around inline tags within
  100.    ## a literal block when line wraps are inserted due to the
  101.    ## outputrecordlength (orl) setting.  Reverse these changes
  102.    ## to ensure the proper formatting for other processing applications
  103.    my($data) = $$Buff;
  104.    $$Buff = '';
  105.  
  106.    while($data =~ m#<(programlisting|screen|literallayout|synopsis)([^>]*>)(.*?)(</\1>)#ms) {
  107.     $$Buff .= $`;
  108.  
  109.     $literal = '<' . $1 . $2 . $3 . $4;
  110.     $data = $';
  111.  
  112.     # If a close tag starts a line within a literal block
  113.     # move it back to the end of the previous line.
  114.     $literal =~ s#([^\n])\n</#$1</#msg;
  115.     $literal =~ s#([^\n])\n\n</#$1\n</#msg;
  116.  
  117.     # If an open tag ends a line within a literal block
  118.     # move the next line to follow immediately after
  119.     $literal =~ s#(<[^/][^>]*?>)\n#$1#msg;
  120.  
  121.     $$Buff .= $literal;
  122.    }
  123.  
  124.    $$Buff .= $data;
  125. }
  126.  
  127. ################################################################################
  128. ### Returns content of a tag pair
  129. ###    $1 = Array index at which to start looking
  130. ###    $2 = tag name (SGML requires case-insensitive match)
  131. ###    $3 = Array in which to search
  132.  
  133. sub GetTagContent {
  134.    my ($I, $Tag, $Buff) = @_ ;
  135.    my ($J, $Content, ) ;
  136.  
  137.    while ( ($$I <= $#$Buff) && ($$Buff[$$I] !~ m<$$Tag\bi) )
  138.    {
  139.       $$I++ ;
  140.    }
  141.    $J = $$I ;
  142.    while ( ($J <= $#$Buff) && ($$Buff[$J] !~ m</$$Tag\bi) )
  143.    {
  144.       $J++ ;
  145.    }
  146.    if ($$I == $J)
  147.    {
  148.       $Content = $$Buff[$$I] ;
  149.    }
  150.    else
  151.    {
  152.       $Content = join (' ', @$Buff[$$I..$J]) ;
  153.    }
  154.    $Content =~ s/\n/ /g ;
  155.    $Content =~ s^.*<$$Tag[^>]*>(.*)</$$Tag.*$1i ;
  156.    $Content =~ s/  +/ /g ;
  157.    $Content =~ s/^ +// ;
  158.    $Content =~ s/ +$// ;
  159.  
  160.    return ($Content) ;
  161. }
  162.  
  163.  
  164. ################################################################################
  165. ### Returns value for an attribute of an element
  166. ###    $1 = Array index at which to start looking
  167. ###    $2 = tag name (SGML requires case-insensitive match)
  168. ###     $3 = attribute name (SGML requires case-insensitive match)
  169. ###    $4 = Array in which to search
  170.  
  171. sub GetAttValue {
  172.    my ($I, $Tag, $AttribName, $Buff) = @_ ;
  173.    my ($J, $TagContent, $AttValue) ;
  174.  
  175.    ### Find the element (tag might be lowercase or uppercase)
  176.    while ( ($$I <= $#$Buff) && ($$Buff[$$I] !~ m<$$Tag\bi) )
  177.    {
  178.       $$I++ ;
  179.    }
  180.    $J = $$I ;
  181.    while ( ($J <= $#$Buff) && ($$Buff[$J] !~ m</$$Tag\bi) )
  182.    {
  183.       $J++ ;
  184.    }
  185.    if ($$I == $J)
  186.    {
  187.       $TagContent = $$Buff[$$I] ;
  188.    }
  189.    else
  190.    {
  191.       $TagContent = join (' ', @$Buff[$$I..$J]) ;
  192.    }
  193.  
  194.    $TagContent =~ s/\n/ /g ;
  195.    $TagContent =~ s^.*<$$Tag([^>]*)>.*$1i ;
  196.    ($AttValue = $TagContent) =~ s^.*\s$$AttribName\s*=\s*\"([^"]*)\".*$1i ;
  197.  
  198.    return ($AttValue) ;
  199.  
  200. }
  201.  
  202.  
  203. ################################################################################
  204. ### Gets information from book SGML file
  205. ###    $1 = Book name; returned by this routine
  206. ###    $2 = Complete publication number; returned by this routine
  207. ###    $3 = Book's version; returned by this routine
  208. ###    $4 = Book's Title; returned by this routine
  209. ###     $5 = Book's shorttitle attribute; returned by this routine
  210. ###     $6 = Book's bookshelf attribute; returned by this routine
  211. ###    $7 = Array of lines from text of book file
  212.  
  213. sub GetBookInfo {
  214.    my ($BookName, $PartNo, $Version, $Title, $ShortTitle,
  215.        $BookShelf, $BuffSGML) = @_ ;
  216.    my ($ClassCode, $Base, $Answer, $I, $AttribName, $Tag,
  217.        $PartInfo, $Tmp, @Tmp, $ChapSuffix) ;
  218.  
  219.    undef $$BookName ;
  220.    undef $$PartNo ;
  221.    undef $ClassCode ;
  222.    undef $Base ;
  223.    undef $$Version ;
  224.  
  225.    undef $$ShortTitle ;
  226.    undef $$BookShelf ;
  227.  
  228.  
  229.    ## DRD Future version:
  230.    ## Need to check value in the SGML file against the one in the Makefile.  If not 
  231.    ## the same, ask user if the value in the Makefile should be used.
  232.    ## Default = yes (need to be able to use Makefile to override values in the SGML file.
  233.  
  234.    if (grep (/<partnumber\b/i, @$BuffSGML))
  235.    {
  236.       $I = 0 ;
  237.       $Tag = 'partnumber' ;
  238.       $PartInfo = &GetTagContent (\$I, \$Tag, $BuffSGML) ;
  239.  
  240.       $I = 0 ;
  241.       $Tag = 'partnumber' ;
  242.       $AttribName = 'shorttitle' ;
  243.       $$ShortTitle = &GetAttValue (\$I, \$Tag, \$AttribName, $BuffSGML) ;
  244.  
  245.       ($ClassCode = $PartInfo) =~ s^.*<classcode[^>]*>(.*)</classcode>.*$\1i ;
  246.       ($Base = $PartInfo) =~ s^.*<base[^>]*>(.*)</base>.*$\1i ;
  247.       ($$Version = $PartInfo) =~ s^.*<version[^>]*>(.*)</version>.*$\1i ;
  248.       $$PartNo = "${ClassCode}-${Base}-$$Version" ;
  249.    }
  250.    else
  251.    {
  252.       print "\n$Name: Only for processing entire book! \n" ;
  253.       exit(1);
  254.    }
  255.  
  256.    ## Get "title" (full title)
  257.    $I = 0 ;
  258.    $Tag = 'title' ;
  259.    $$Title = &GetTagContent (\$I, \$Tag, $BuffSGML) ;
  260.    $$Title =~ s/\&[^;]+;//g ;  ### Remove any character entities
  261.  
  262.    $$BookName = $$PartNo ;
  263. }
  264.  
  265.  
  266.  
  267.  
  268. ################################################################################
  269. #####                                MAIN                                  #####
  270. ################################################################################
  271.  
  272.  
  273. $[ = 0 ;
  274. ($Name = $0) =~ s^.*/ ;
  275. select((select(STDOUT), $| = 1)[0]) ;   ### No buffering of stdout for messages
  276. $Usage = "
  277. Usage:  $Name  [-draft] [-numheads] -I<inputfile> -O<outputfile>
  278.  
  279.     -draft  
  280.                 Draft version; keep the comments and revision indicators
  281.                 for reviewers in displayed version (InSight or the
  282.                 'post4review' DynaWeb copies).
  283.  
  284.     -numheads
  285.         Inserts the numheads=Y attribute on the <sgidocbk>
  286.         tag for use by the stylesheets
  287.  
  288.     <inputfile>
  289.         Name of the .tmp SGML file generated by 'spam' in the
  290.                 current book's working directory.
  291.  
  292.         <outputfile>
  293.                 Name of the final buildable source file, by convention
  294.                 the <shorttitle>.sgml name.  
  295.  
  296.         Example:
  297.  
  298.            $Name -draft -IFSafe_IG.sgml.tmp -OFSafe_IG.sgml
  299. " ;
  300.  
  301. ## definitions
  302. $DraftVersion = 0 ;   ## flag for draft or final (default is final; strip <comment> elements)
  303. $NumberedHeads = 0;
  304.  
  305. $Indexer = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/indexgen_sgidocbk";
  306. $XformSGML_public = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/xformsgml_public";
  307.  
  308. $TmpFile = "tmp_make_bldsgml.$$" ;    ## temporary file(s) created and deleted
  309.                                       ## by this script
  310.  
  311. if (! @ARGV) {
  312.    print STDERR "$Usage";
  313.    exit(1);
  314. }
  315.  
  316. while (@ARGV) {
  317.    $Arg = shift (@ARGV) ;
  318.    if ("$Arg" eq '-draft')
  319.    {
  320.       $DraftVersion = 1 ;
  321.    }
  322.    elsif ($Arg eq '-numheads')
  323.    {
  324.       $NumberedHeads = 1;
  325.    }
  326.    elsif ($Arg =~ /^-I.+/)
  327.    {
  328.       $Arg =~ s/^-I// ;
  329.       push (@Files, "$Arg") ;
  330.       $InputFile = $Arg ;
  331.    }
  332.    elsif ($Arg =~ /^-O.+/)
  333.    {
  334.       $Arg =~ s/^-O// ;
  335.       push (@Files, "$Arg") ;
  336.       $OutputFile = $Arg ;
  337.    }
  338.    elsif ($Arg =~ /^-/) {
  339.       print STDERR "\n\aUnrecognized option, '$Arg'$Usage" ;
  340.       exit(1);
  341.    }
  342. }
  343.  
  344.  
  345. if ("$InputFile" eq '') {
  346.    print STDERR "$Usage" ;
  347.    exit(1);
  348. }
  349.  
  350. if ("$OutputFile" eq '') {
  351.    print STDERR "$Usage" ;
  352.    exit(1);
  353. }
  354.  
  355. if (! @Files) {
  356.    print STDERR "$Usage" ;
  357.    exit(1);
  358. }
  359.  
  360. if (! $DraftVersion) {
  361.     print "\nPreparing buildable source SGML for FINAL version.\n";
  362. } else {
  363.     print "\nPreparing buildable source SGML for DRAFT version.\n";
  364. }
  365.  
  366.    ### Clean up the SGML instance:
  367.    print "Preparing SGML instance for processing.\n" ;
  368.    if (! open (FileI, "$InputFile")) {
  369.       print STDERR "\a\n$Name:  $InputFile\n" ;
  370.       exit(16);
  371.    }
  372.    $Buff = join('', <FileI>);
  373.    close (FileI) ;
  374.    $FileName = "$InputFile" ;
  375.    &CleanUp (\$FileName, \$Buff) ;
  376.  
  377.    if (! (open (FileZ, "> $TmpFile"))) {
  378.       print STDERR "\a\n$Name:  $TmpFile\n" ;
  379.       exit(13);
  380.    }
  381.    print FileZ join ('', $Buff) ;
  382.    close (FileZ) ;
  383.  
  384.    ### Make public version of file if not making draft version.
  385.    ##  Replace TmpFile with public version. 
  386.    if (! $DraftVersion) {
  387.       $ErrorCode = system ("$XformSGML_public -I$TmpFile -O$TmpFile.2");
  388.       if (! $ErrorCode) {
  389.          system ("rm -f $TmpFile");
  390.          system ("mv $TmpFile.2 $TmpFile");
  391.       }
  392.    }
  393.  
  394.    ### Build the writer's (back-of-book) index:
  395.    ##  (This is specific for Insight.)
  396.    print "Generating book index.\n" ;
  397.    &RunIt ("$Indexer -i $TmpFile -o $TmpFile.idx") ;
  398.  
  399.    ### Put generated index into SGML instance:
  400.    print "Integrating book index.\n";
  401.  
  402.  
  403.    ##  Get the book info from the SGML instance:
  404.    if (! open (FileA, "$TmpFile")) {
  405.       print STDERR "\a\n$Name:  $TmpFile\"\n" ;
  406.       exit(16);
  407.    }
  408.    undef $/ ;
  409.    @BuffSGML = <FileA> ;
  410.    close (FileA) ;
  411.    $/ = "\n" ;
  412.    &GetBookInfo (\$BookName, \$PartNo, \$Version, \$Title,
  413.                  \$ShortTitle, \$BookShelf, \@BuffSGML) ;
  414.  
  415.    ### Compare shorttitle from SGML with value from Makefile:
  416.    ($InputShortTitle = $OutputFile) =~ s#(.*).sgml#\1#;
  417.  
  418.    if ("$InputShortTitle" ne "$ShortTitle")
  419.    {
  420.       print "\n\nCAUTION! The short title in the Makefile is different" ;
  421.       print "\nthan the value in the SGML file." ;
  422.       print "\n  Short title from Makefile = $InputShortTitle " ;
  423.       print "\n  Short title from SGML = $ShortTitle " ;
  424.       print "\nContinuing, but using value from Makefile.\n\n" ;
  425.    }
  426.  
  427.    ### Open the "indexless" SGML instance:
  428.    if (! open (FileS, "$TmpFile")) {
  429.       print STDERR "\a\n$Name:  $TmpFile.2\"\n" ;
  430.       exit(16);
  431.    }
  432.    $Buff1 = join('', <FileS>);
  433.    close (FileS);
  434.  
  435.    ##  Open the index file:
  436.    if (! open (FileB, "$TmpFile.idx")) {
  437.       print STDERR "\a\n$Name:  $TmpFile.idx\"\n" ;
  438.       exit(16);
  439.    }
  440.    $Buff2 = join('', <FileB>);
  441.    close (FileB);
  442.  
  443.    ###  Insert the index into the SGML file:
  444.    $Buff1 =~ s#<index>\s*</index>#$Buff2#i;
  445.  
  446.    ### Change shorttitle attribute value to Makefile value:
  447.    $Buff1 =~ s#<partnumber([^>]*)shorttitle\s*=\s*\"([^"]+)\"#<partnumber$1shorttitle=\"$InputShortTitle\"#ig ;
  448.  
  449.    # add the NUMHEADS attribute to <sgidocbk> if needed
  450.    if($NumberedHeads == 1) {
  451.     $Buff1 =~ s/(<sgidocbk)/\1 NUMHEADS="Y"/i;
  452.    }
  453.  
  454.    ##  Open the final shorttitle.sgml output file:
  455.    if (! (open (FileZ, "> $OutputFile"))) {
  456.       print STDERR "\a\n$Name:  $Outputfile\n" ;
  457.       exit(13);
  458.    }
  459.    print FileZ $Buff1;
  460.    close (FileZ);
  461.  
  462.    ### Remove temporary files:
  463.    unlink "$TmpFile" ;
  464.    unlink "$TmpFile.idx" ;
  465.  
  466. print "Buildable source SGML done.\n\n" ;
  467.